package com.flower.subject.application.controller;


import com.flower.subject.application.convert.SubjectCategoryDTOConverter;
import com.flower.subject.application.dto.SubjectCategoryDTO;
import com.flower.subject.domain.entity.SubjectCategoryBO;
import com.flower.subject.domain.service.SubjectCategoryDomainService;
import com.flower.subject.entity.Result;
import com.flower.subject.infra.basic.entity.SubjectCategory;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-02 18:06
 */
@RestController
@RequestMapping("category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if(log.isInfoEnabled()){

        }
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANT.convertBoTOCategory(subjectCategoryDTO);
        subjectCategoryDomainService.add(subjectCategoryBO);

        return Result.ok(true);
    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(){
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setParentId(0);
        List<SubjectCategoryBO> subjectCategoryBOS = subjectCategoryDomainService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANT.convertBoTOCategoryDTOList(subjectCategoryBOS);

        return Result.ok(subjectCategoryDTOS);

    }


    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){

        Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类ID不能为空");
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANT.covertDTOtoCategory(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOS = subjectCategoryDomainService.queryCateGory(subjectCategoryBO);


        return Result.ok(subjectCategoryBOS);

    }


    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){

            log.info("update category:{}",subjectCategoryDTO);
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getId(),"ID不能为空");
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANT.covertDTOtoCategory(subjectCategoryDTO);
        Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);


        return Result.ok(result);

    }


    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){

            log.info("update category:{}",subjectCategoryDTO);
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getId(),"ID不能为空");
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANT.covertDTOtoCategory(subjectCategoryDTO);
        Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);


        return Result.ok(result);

    }
}
